/**
 * 15/02/2014
 * 
 * CustomerController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CustomerBOInterface;
import org.cuatrovientos.erp.model.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class for Customer management
 * @author Javier Tejedor
 */
@Controller
public class CustomerController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(CustomerController.class);

	@Autowired
	private CustomerBOInterface customerBO;

	/**
	 * default constructor
	 */
	public CustomerController () {
		logger.info("Customer BEAN instantiated");
	}

	/**
	 * handles default /customer or /customer/list request
	 * @return
	 */
	@RequestMapping({"/customer","/customer/list"})
	public ModelAndView customerDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Customer> customerList = customerBO.selectAll();
		model.put("cusmtomers", customerList );
		
		return new ModelAndView("CustomerList", model);     
	}


	/**
	 * handles default /customer/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/customer/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true)
			String name) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Customer> customerList = customerBO.search(name);
		model.put("customers", customerList);
		
		return new ModelAndView("CustomerList", model);     
	}

	/**
	 * handles default /customer/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/customer/delete"})
	public String deleteCustomer(
			@RequestParam(value="id", defaultValue="", required=true) int id) {
		
		logger.info("delete " + id);

		customerBO.deleteById(id);
		
		return "redirect:/customer";
	}

	/**
	 * handles default /customer/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/customer/new"})
	public ModelAndView newCustomer() {
		logger.info("new customer");
		
		Customer customer = new Customer();

		return new ModelAndView("NewCustomer", "customer", customer);
	}

	/**
	 * handles default /customer/save request
	 * @param customer
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/customer/save"})
	public String newCustomer(@Valid Customer customer, BindingResult bindingResult) {
		logger.info("new customer " + customer.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("customer", customer);
			
			return "NewCustomer";
		}

		customerBO.insert(customer);
		
		return "redirect:/customer";
	}

	/**
	 * handles default /customer/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/customer/detail"})
	public ModelAndView customerDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Customer customer = customerBO.selectById(id);
		model.put("customer", customer);

		return new ModelAndView("CustomerDetail", model);
	}


	/**
	 * handles default /customer/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/customer/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Customer customer = customerBO.selectById(id);
		model.put("customer", customer);
		
		return new ModelAndView("UpdateCustomer",model);
	}


	/**
	 * handles default /customer/saveupdate request
	 * @param customer
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/customer/saveupdate"})
	public String saveUpdate(@Valid Customer customer, BindingResult bindingResult) {
		logger.info("update customer " + customer.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("customer", customer);
			
			return "UpdateCustomer";
		}

		customerBO.update(customer);
		
		return "redirect:/customer";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/customer/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexCustomer";
	}
	
}
